#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use LWP::UserAgent;

my $cgi=new CGI;

my %FormValues;

 print "Content-Type: text/html\n\n";
#print "Content-Type: application/pdf\n";
#print "Content-Description: pdf file of access statistics;\n";

#  print "Content-Disposition: attachment; filename=${file}\n\n";
#  print "Content-Disposition: filename=${file}\n\n";


my $url = 'https://www.google.com/accounts/ServiceLoginBoxAuth';

my $blog = $cgi->param("blog");

exit(0) if not $blog;


$FormValues{"continue"}="http://www.google.com/analytics/home/?et=reset&amp;hl=en-US";
$FormValues{"continue"}="https://www.google.com/analytics/reporting/dashboard?reset=1&id=4873400&scid=2718547";
$FormValues{"continue"}="https://www.google.com/analytics/reporting/export?fmt=1&id=5142326&pdr=20071104-20071204&cmp=average&rpt=ContentByTitleReport&q=$blog&qtyp=1";
$FormValues{"service"}="analytics";
$FormValues{"nui"}="1";
$FormValues{"hl"}="en-US";
$FormValues{"GA3T"}="ELalTfN07Ik";
$FormValues{"GALX"}="56IO80BMVPQ";
$FormValues{"Email"}="reader.report";
$FormValues{"Passwd"}="Ling8026";
$FormValues{"PersistentCookie"}="yes";
$FormValues{"null"}="Signin";

if(0){

print "<html> <head><LINK REL='stylesheet' HREF='http://www.jp-life.org/wiki.css'> </head> <body> <br>
<blockquote> \n";


print "In the code, three vriables <b>have</b> been sent out to the given url. Also please notice that some hidden vaule is also transfered!<br>\n";
print "<li> id=".$FormValues{'id'}."</li>\n";
print "<li> pw=".$FormValues{'pw'}."</li>\n";
print "<li> ipw=".$FormValues{'ipw'}."</li>\n";
print "<li> sent to: ".$url."</li>\n";

print "Response from the give url:
</blockquote><hr> <pre>\n <blockquote>\n";

}


if(not $url ){
	print "No action link.<br>"; exit();
}


my $agent=$ENV{'HTTP_USER_AGENT'};

my $ua = LWP::UserAgent->new;

use HTTP::Cookies;

  $ua->cookie_jar( HTTP::Cookies->new( 'file' => '/home/xfliu/cookies.lwp',
        # where to read/write cookies
    'autosave' => 1,
        # save it to disk when done
  ));

$ua->agent($agent);

my $response = $ua->post( $url, \%FormValues );

$response->is_success or  die "Failed to GET '$url': ", $response->status_line;

my $result= $response->as_string;


$url="https://www.google.com/analytics/reporting/export?fmt=0&id=5142326&pdr=20071104-20071204&cmp=average&rpt=ContentByTitleReport&q=%E6%96%B9%E8%88%9F%E5%AD%90&qtyp=0";
$url="https://www.google.com/analytics/reporting/content_titles?id=5142326&pdr=20071104-20071204&cmp=average&trows=50";

#https://www.google.com/analytics/reporting/dashboard?reset=1&id=4873400&scid=2718547";
$response = $ua->post( $url, \%FormValues );
$result= $response->as_string;

my $ok=1;


my @re_array=split(/\n/, $result);
my $error;
my $start=0;
foreach (@re_array){
	#Here, you can deal with each line of the response, which is just a comman webpage.

#	s/="\/analytics/="http:\/\/www.google.com\/analytics/g;
        s/href="([^"\/]*)"/href=""/g;


	$start=1 if /Content-Disposition/; 
	print $_ if $start;
	print "\n" if $start;
	$start=1 if /^$/; 
}




if (0){


print "</pre></blockquote>";

	open FH, "<login_2.html";

	foreach (<FH>){
		
		if( /\[\[(.+)\]\]/ ){

			my $tt=$FormValues{$1};
			$tt=~s/\s//g;
			
			my $value=$cgi->param($tt);
			$_=~ s/\[\[(.*)\]\]/$value $tt/g;
		}	
		print $_,"\n";
		
	}

}

# end;
